/*
Copyright 2025 PerfXLab (Beijing) Technologies Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "OpenSpB_Alloc.h"
#include "OpenSpB_Type.h"
#include "OpenSpB_Scalar.h"
#include "OpenSpB_Error.h"

OpenSpB_info OpenSpB_scalar_new(
    OpenSpB_scalar *scalar,
    OpenSpB_type d)
{
    *scalar = (struct OpenSpB_scalar_opaque *)
        OpenSpB_aligned_malloc(
            sizeof(struct OpenSpB_scalar_opaque));
    if (*scalar == NULL)
    {
        return OpenSpB_failure;
    }
    (*scalar)->element_type = d;
    return OpenSpB_success;
}

OpenSpB_info OpenSpB_scalar_free(OpenSpB_scalar *scalar)
{
    if (*scalar == NULL)
    {
        return OpenSpB_failure;
    }
    else
    {
        free(*scalar);
        return OpenSpB_success;
    }
}

#define CASE_SETZERO(type, T)          \
    case OpenSpB_##T:                      \
        s->value.T##_value = ZERO_##T; \
        break;

#define CASES_SETZERO               \
    CASE_SETZERO(bool, BOOL);       \
    CASE_SETZERO(int8_t, INT8);     \
    CASE_SETZERO(int16_t, INT16);   \
    CASE_SETZERO(int32_t, INT32);   \
    CASE_SETZERO(int64_t, INT64);   \
    CASE_SETZERO(uint8_t, UINT8);   \
    CASE_SETZERO(uint16_t, UINT16); \
    CASE_SETZERO(uint32_t, UINT32); \
    CASE_SETZERO(uint64_t, UINT64); \
    CASE_SETZERO(float, FP32);      \
    CASE_SETZERO(double, FP64);

OpenSpB_info OpenSpB_scalar_setZero(OpenSpB_scalar s)
{
    switch (s->element_type)
    {
        CASES_SETZERO;

    default:
        OpenSpB_Error("Unknown element type \
in the OpenSpB_scalar_setZero!\n",
                  __FILE__, __LINE__);
        break;
    }
    return OpenSpB_success;
}
#undef CASES_SETZERO
#undef CASE_SETZERO
